using System;
using Csla;
using ENT.PM.Datos.MysqlHelper;
using System.Data;

namespace ENT.PM.Core
{
    [Serializable]
    public class RegionLookup : NameValueListBase<int, string>
    {
        #region Factory Methods

        private static RegionLookup _list;
        /// <summary>
        /// Obtiene una lista de Regiones del Ecuador y Regionales del Senplades
        /// </summary>
        /// <param name="Codigo">Codigo del catalogo de Region y Regionales </param>
        /// <returns></returns>
        public static RegionLookup GetNameValueList(string Codigo)
        {
            if (_list == null)
                _list = DataPortal.Fetch<RegionLookup>(new SingleCriteria<RegionLookup, string>(Codigo));
            return _list;
        }
        public static RegionLookup GetNameValueList(int idPais)
        {
            if (_list == null)
                _list = DataPortal.Fetch<RegionLookup>(new SingleCriteria<RegionLookup,int>(idPais));
            return _list;
        }

        public static void InvalidateCache()
        {
            _list = null;
        }

        private RegionLookup()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void DataPortal_Fetch(SingleCriteria<RegionLookup, string> criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", criteria.Value);
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Listar_Catalogo_Codigo").Tables[0].Rows)
            {
                this.Add(new NameValueListBase<int, string>.NameValuePair(Convert.ToInt32(items[0]), items[2].ToString()));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }
        private void DataPortal_Fetch(SingleCriteria<RegionLookup,int> idPais)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDPAPA", idPais.Value);
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Listar_Catalogo_Padre").Tables[0].Rows)
            {
                this.Add(new NameValueListBase<int, string>.NameValuePair(Convert.ToInt32(items[0]), items[2].ToString()));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }
}
